//
//  UILabel+Category.m
//  YLB
//
//  Created by Zoo Chow on 17/02/2017.
//  Copyright © 2017 Zoo. All rights reserved.
//

#import "UILabel+Category.h"

@implementation UILabel (Category)

+ (UILabel*)createLabel:(CGRect)frame tag:(NSInteger)tag text:(NSString *)text color:(UIColor *)color font:(UIFont *)font {
    UILabel * label = [[UILabel alloc]initWithFrame:frame];
    [label setTag:tag];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];
    [label setText:text];
    [label setTextColor:color];
    [label setFont:font];
    return label;
}

+ (UILabel*)createLabel:(CGRect)frame tag:(NSInteger)tag attributedString:(NSAttributedString *)attributedString {
    UILabel * label = [[UILabel alloc]initWithFrame:frame];
    [label setTag:tag];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];
    [label setAttributedText:attributedString];
    return label;
}

+ (CGSize)getLabelSizeToFitInBaseSize:(CGSize)baseSize inFont:(UIFont *)font withText:(NSString *)text {
    if ([text length]) {
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, baseSize.width, baseSize.height)];
        [label setLineBreakMode:NSLineBreakByWordWrapping];
        [label setNumberOfLines:0];
        [label setText:text];
        [label setFont:font];
        [label sizeToFit];
        CGSize size = label.bounds.size;
        label = nil;
        return size;
    }
    return CGSizeZero;
}

+ (CGSize)getLabelSizeToFitInBaseSize:(CGSize)baseSize inFont:(UIFont *)font withText:(NSString *)text textAlignment:(NSTextAlignment)alignment {
    if ([text length]) {
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, baseSize.width, baseSize.height)];
        [label setLineBreakMode:NSLineBreakByWordWrapping];
        [label setNumberOfLines:0];
        [label setTextAlignment:alignment];
        [label setText:text];
        [label setFont:font];
        [label sizeToFit];
        CGSize size = label.bounds.size;
        label = nil;
        return size;
    }
    return CGSizeZero;
}

- (CGSize)getSizeToFitInBase {
    if ([self.text length]) {
        UILabel * label = [[UILabel alloc]initWithFrame:self.bounds];
        [label setLineBreakMode:NSLineBreakByWordWrapping];
        [label setNumberOfLines:0];
        [label setText:self.text];
        [label setFont:self.font];
        [label sizeToFit];
        CGSize size = label.bounds.size;
        label = nil;
        return size;
    }
    return CGSizeZero;
}

- (void)setTextWithSource:(NSArray *)sources {
    NSNumber * fontString = [self.font.fontDescriptor objectForKey:@"NSFontSizeAttribute"];
    CGFloat height = fontString.floatValue+4;
    NSMutableAttributedString * attributed = [[NSMutableAttributedString alloc]initWithString:@""];
    [sources enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UIImage class]]) {
            NSTextAttachment * attachment = [[NSTextAttachment alloc]init];
            attachment.image = (UIImage*)obj;
            attachment.bounds = CGRectMake(0, -4, height, height);
            NSAttributedString * ats = [NSAttributedString attributedStringWithAttachment:attachment];
            [attributed appendAttributedString:ats];
        } else if ([obj isKindOfClass:[NSString class]]) {
            NSAttributedString * ats = [[NSAttributedString alloc] initWithString:(NSString *)obj];
            [attributed appendAttributedString:ats];
        }
    }];
    self.attributedText = attributed;
}

@end































